package com.kkwrite.regimen.consumer.controller.join;

import java.util.Calendar;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import com.kkwrite.regimen.common.entity.dto.InDTO;
import com.kkwrite.regimen.common.entity.dto.OutDTO;
import com.kkwrite.regimen.common.entity.dto.apply.ApplyJoinInDTO;
import com.kkwrite.regimen.consumer.controller.constant.Constant;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 加盟模块
 * 
 * @author Ke.Wang
 * @date 2018年7月17日 上午11:26:45
 */
@Api(value = "加盟模块")
@RestController
@RequestMapping("/join")
public class JoinController {

	private final static Logger logger = LoggerFactory.getLogger(JoinController.class);
	
	@Resource(name = "restTemplate")
	private RestTemplate restTemplate;
	
	/**
	 * 申请加盟
	 */
	@ApiOperation(value = "申请加盟接口")
	@ApiImplicitParams(
		{ 
			@ApiImplicitParam(name = "proposer", value = "申请人", required = true, paramType = "body", dataType = "string"),
			@ApiImplicitParam(name = "phoneNum", value = "手机号码", required = true, paramType = "body", dataType = "string"),
			@ApiImplicitParam(name = "sex", value = "性别", required = true, paramType = "body", dataType = "string"),
			@ApiImplicitParam(name = "applyJoinTime", value = "申请时间", required = true, paramType = "body", dataType = "string"),
			@ApiImplicitParam(name = "applyJoinNote", value = "申请说明", required = true, paramType = "body", dataType = "string")
		}
	)
	@SuppressWarnings("rawtypes")
	@PostMapping("/apply")
	public OutDTO apply(@RequestBody InDTO<ApplyJoinInDTO> inDTO) {
		ApplyJoinInDTO applyJoinInDTO = inDTO.getBody();
		applyJoinInDTO.setApplyJoinTime(Calendar.getInstance().getTime()); // TODO 临时赋值

		logger.info("JoinController.applyJoin(), applyJoinInDTO = {}", applyJoinInDTO);
		if (logger.isDebugEnabled()) {
			logger.debug("JoinController.applyJoin(), applyJoinInDTO = {}", applyJoinInDTO);
		}
		
		String url = Constant.SERVICE_PREFIX + "/join/apply";
		try {
			return restTemplate.postForObject(url, applyJoinInDTO, OutDTO.class);
		} catch (RestClientException e) {
			logger.error("JoinController.apply(), applyJoinInDTO = " + applyJoinInDTO + "_" + e.getMessage());
			e.printStackTrace();
			return new OutDTO("failed", "申请加盟失败");
		}
	}
}
